package com.nuo.modules.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nuo.common.excel.po.ExportListPo;
import com.nuo.common.excel.service.ExcelService;
import com.nuo.common.exception.GlobalException;
import com.nuo.common.utils.*;
import com.nuo.modules.student.po.LoginPo;
import com.nuo.modules.student.po.PwdPo;
import com.nuo.modules.sys.service.SysUserTokenService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuo.modules.student.dao.StudentDao;
import com.nuo.modules.student.entity.StudentEntity;
import com.nuo.modules.student.service.StudentService;
import org.springframework.web.multipart.MultipartFile;



@Service("studentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

    @Autowired
    private ExcelService<StudentEntity, StudentService> excelService;

    @Autowired
    SysUserTokenService sysUserTokenService;

    @Autowired
    InfoUtils<StudentEntity> infoUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String username =(String) params.get("username");
        IPage<StudentEntity> page = this.page(
                new Query<StudentEntity>().getPage(params),
                new QueryWrapper<StudentEntity>().lambda()
                        .like(StringUtils.isNotBlank(username), StudentEntity::getUsername, username)
        );
        return new PageUtils(page);
    }

    @Override
    public R login(LoginPo po) {
        StudentEntity student = getOne(new LambdaQueryWrapper<StudentEntity>()
                .eq(StudentEntity::getStudentId, po.getStudentId())
                .eq(StudentEntity::getPassword, new Sha256Hash(po.getPassword(), Constant.SSALT).toHex()));
        if (student != null) {
            return sysUserTokenService.createToken(student.getId(), 1);
        } else {
            return R.error(404, "学号或密码输入有误...");
        }
    }

    @Override
    public R importList(MultipartFile file) {
        return excelService.importList(file, StudentEntity.class, this);
    }

    @Override
    public R exportList(ExportListPo<StudentEntity> po) {
        return excelService.exportList(po, StudentEntity.class);
    }

    @Override
    public R exportAll() {
        return excelService.exportAll(StudentEntity.class, this);
    }

    @Override
    public R info(String token) {
        return R.ok().put("student", infoUtils.getInfoByToken(token, 1, this));
    }

    @Override
    public R changePwd(PwdPo po) {
        StudentEntity student = infoUtils.getInfoByToken(po.getToken(), 1, this);
        if (!PwdUtils.isOk(po.getOldPassword()) || !PwdUtils.isOk(po.getNewPassword())) {
            throw new GlobalException("密码需为数字+字母6位");
        }
        String oldPassword = new Sha256Hash(po.getOldPassword(), Constant.SSALT).toHex();
        String newPassword = new Sha256Hash(po.getNewPassword(), Constant.SSALT).toHex();
        if (!Objects.equals(oldPassword, student.getPassword())) {
            return R.error("原密码输入有误...");
        }
        StudentEntity studentEntity = new StudentEntity();
        studentEntity.setPassword(newPassword);
        studentEntity.setId(student.getId());

        updateById(studentEntity);
        return R.ok();
    }

    @Override
    public void saveOrUpdateBatch(List<StudentEntity> list, Class<StudentEntity> clazz) {
        saveOrUpdateBatch(list, "student_id", "studentId", StudentEntity.class);
    }

}
